home *** CD-ROM | disk | FTP | other *** search
- Path: mudskipper.cac.psu.edu!user
- From: fcusack@tdx.org (frank.)
- Newsgroups: comp.lang.c++
- Subject: Re: Static Member Functions
- Date: Mon, 19 Feb 1996 07:17:07 -0400
- Organization: Psychic Enemies Network
- Message-ID: <fcusack-1902960717070001@mudskipper.cac.psu.edu>
- References: <31277E66.687F@iastate.edu>
- NNTP-Posting-Host: mudskipper.cac.psu.edu
-
- In article <31277E66.687F@iastate.edu>, Steve Lee <sjlee@iastate.edu> wrote:
-
- > Hi,
- >
- > I have a couple of questions about static member functions.
- >
- > 1) I was wondering why C++ doesn't provide (or maybe it does and I
- don't) a way to protect a
- > static member function by making it constant.
-
- What do you mean by "protect a static member function"? from what?
-
- > What I had in mind was something like the usage of
- > const for a member function. I know that in this sense, it is making
- the implicit variable *this
- > constant,
-
- Do you mean the implicit variable this (as opposed to *this)? It also
- makes the data in the object pointed to by this constant.
-
- "The type of _this_ in a _const_ member function of class _X_ is
- _const X *const_." [Stroustrup, 2nd Ed, p. 148; see also p. 547]
-
- > and that static member functions don't have an implicit *this variable
- since they don't
-
- Neither do they have an implicit this variable. :)
-
- > act on a specific object. I want to know why this, or something similar
- isn't possible.
- >
- > class foo {
- >
- > static int GetCount() const;
-
- ???
-
- since GetCount() is a static member function, it does not have a this
- variable; it does not act on data from a specific instance of class foo.
- What would the _const_ refer to?? Here, GetCount() does not take any
- parameters [which BTW I prefer to declare as 'GetCount(void)'] so it
- cannot modify data from any instance of foo - it does not have access to
- an object of class foo.
-
- It _can_ modify static variables like count, below, but count is not an
- instance variable, so again, the _const_ declaration has no meaning.
-
- If you intend to pass an object of class foo to GetCount(), simply declare
- the parameter _const_ to avoid modification.
-
- static int GetCount(const foo &p1);
-
- > static int count;
- > };
- >
- > 2) In the example above, count is private. I find it interesting
- though that any piece of code
- > can initialize the private variable count. Isn't this contradictory?
-
- Regardless of whether any piece of code can initialize/define count, since
- count is a private member, only member functions and friends of class foo
- may modify it.
-
- > The only thing I can think
- > of is that the linker only allows it to be initialized once (obviously),
- and if the implementor
- > of the class initializes it, then no users will be able to without a
- linker error.
- >
-
- Not quite. static data members _must_ be defined/initialized exactly once;
- not "allowed to be [defined] once" as you put it. Thus the implementor
- _must_ define it, not "if the implementor...".
-
- That is, assuming you are given a library. If you are given the source
- code to use, then the user can of course change the definition anyway.
- ~Frank
- -- I am Pentium of Borg. Division is futile. You will be approximated. --
- -- If you build it, they will come --> http://www.tdx.org/~fcusack/ --
- -- PGP key fingerprint: 01 C0 C0 B9 CC 78 67 0F 3F 64 80 65 8B 0F F9 EA --
-